//
//  Message.h
//  WiFiMACSim
//
//  Created by Christopher Kemsley on 3/31/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#ifndef WiFiMACSim_Message_h
#define WiFiMACSim_Message_h

#include <ostream>
#include <string>
#include "Types.hpp"

SimNamespaceBegin


struct Message
{
	// Enumerations
	
	typedef enum {
		kTypeData ,
		kTypeRTS ,
		kTypeCTS ,
		kTypeACK
	} Type ;
	
	typedef enum {
		kStatusUnhandled = 1,		// Nothing has happened by and receiver
		kStatusSuccessful = 0,		// Receiver received it in whole
		kStatusFailedToReceive = 2	// Receiver failed on RX
	} Status ;
	
	static std::string const&	typeString ( Type const & type ) ;
	static std::string const&	statusString ( Status const & status ) ;
	
	
	
	// Identity
	
	ID const	sender ;
	ID const	target ;
	Type const	type ;
	
	
	// Slot count
	// Messages are in flight in the double-sided-inclusive range [firstSlot,lastSlot]
	// Do not adjust these properties yourself!
	
	UInteger const	duration ;
	UInteger		firstSlot ;	// UIntegerMax until registered
	UInteger		lastSlot ;	// UIntegerMax until registered
	bool			isInFlight ;
	
	UInteger		remainingDuration __attribute__((unavailable)) ; // in slots
	
	
	// Status
	
	Status		status ;
	
	
	
	
	// Creation
	
	Message ( ID const & inSender , ID const & inTarget , Type inType , UInteger inDuration ) ;
	
	
	// Reference Counting
	
	void	retain () ;
	void	release () ;
	
	
	friend std::ostream & operator<< ( std::ostream & str , Message const & msg ) ;
	
private:
	UInteger	_retainCount ;
	~Message () ;
} ;


SimNamespaceEnd
#endif
